<?php

class Userv2_model extends Base_Model
{
    const TABLE = 'user_v2';

    public function __construct()
    {
        $this->load->database();
    }

    /**
     * 创建会员
     *
     * @param $data
     * @return bool|int
     */
    public function createUser($data)
    {
        if($this->db->insert(self::TABLE, $data)){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * 修改会员
     *
     * @param $data
     * @param $where
     * @return CI_DB_active_record|CI_DB_result
     */
    public function editUser($data, $where)
    {
        return $this->db->update(self::TABLE, $data, $where);
    }

    /**
     * 获取userv2信息
     *
     * @param $where
     * @return mixed
     */
    function getUserv2($where, $data = '*')
    {
        $query = $this->db->select($data)->get_where(self::TABLE, $where);
        return $query->row_array();
    }

    /**
     * 检查用户
     *
     * @param $mobile
     * @param $email
     * @return array
     */
    function checkUser($email,$password,$uType)
    {
        $query = $this->db->select('uid, active, status, utype')
                          ->from(self::TABLE)
                          ->where('(telNum ="' .$email.'" or userId = "'.$email.'")')
                          ->where(array('userToken'=>md5($password)))
                          ->where(array('utype' => $uType))
                          ->get()->row_array();
        if(!empty($query)){
        	if($query['active']!=1)
        	{return array('status'=>false,'msg'=>'您的账户未激活','uid'=>'','utype'=>$query['utype']);}
        	else {
        		if($query['status']==99)
        			return array('status'=>false,'msg'=>'您的账户因为相关原因已被屏蔽','uid'=>'','utype'=>$query['utype']);
        		else 
        			return array('status'=>true,'msg'=>'登陆成功','uid'=>$query['uid'], 'utype'=>$query['utype']);
        	}
        }else {
        	return array('status'=>false,'msg'=>'你输入的密码或是邮箱地址/手机号码错误','uid'=>'','utype'=>'');
        }
        

    }

    /**
     * 获取user信息
     *
     * @param $where
     * @return array
     */
    function getUser($where)
    {
        return $this->db->get_where(self::TABLE, $where)->row_array();
    }
    /**
     * 判定user是否登录
     *
     * @param $uid
     * @return bool
     */
    function checkUserLogin($uid)
    {
    	$this->load->driver('cache',array('adapter' => 'apc', 'backup' => 'file'));
    	$cache_id = 'login_'.$uid;
    	$login=$this->cache->get($cache_id);
    	if(!empty($login)){
    		return $login;
    	}else{
    		return NULL;
    	}
    }

}
